package Gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import auth.UserFuncs;

public class Register {
	public Display regDisplay;
	private Shell regShell;
	public Register(Shell shell, final Main main){
		this.regDisplay = shell.getDisplay();
		this.regShell = new Shell (regDisplay);
		main.addShell(regShell);
		invokeRegister(shell, regShell, regDisplay, main);
	}
	public static void invokeRegister(Shell shell, final Shell regShell, Display regDisplay, final Main main) {
		GridLayout gl = new GridLayout(1,true);
		regShell.setLayout(gl);
		regShell.setText("Register");
		regShell.setLocation(shell.getLocation().x + 50, shell.getLocation().y + 50);
	
		Label Explain = new Label(regShell, SWT.NONE);
		Explain.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,true));
		Explain.setText("Please fill in the following details:");
		
		Label Name = new Label (regShell, SWT.NONE);
		Name.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Name.setText ("Name:\t");
		final Text NameInput = new Text(regShell,SWT.BORDER);
		NameInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
	
		Label Email = new Label (regShell, SWT.NONE);
		Email.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Email.setText ("Email:\t");
		final Text EmailInput = new Text(regShell,SWT.BORDER);
		EmailInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
	
		Label Username = new Label (regShell, SWT.NONE);
		Username.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Username.setText ("Username:\t");
		final Text UsernameInput = new Text(regShell,SWT.BORDER);
		UsernameInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
	
		Label Password = new Label (regShell, SWT.NONE);
		Password.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Password.setText ("Password:\t");
		final Text PasswordInput = new Text(regShell,SWT.BORDER);
		PasswordInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		PasswordInput.setEchoChar('*');
	
		final Button regBt = new Button(regShell,0);
		regBt.setText("Register");
		regBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		final Button cancelBt = new Button(regShell,0);
		cancelBt.setText("Cancel");
		cancelBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		Listener listener = new Listener () {
			public void handleEvent (Event e) {
				switch (e.type) {
				case SWT.Close:
					main.removeShell(regShell);
					main.enableLogin();
					main.enableReg();
					regShell.dispose();
					break;
				default:
		        }	
		   }
		};
		regShell.addListener (SWT.Close, listener);
		
		cancelBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						main.removeShell(regShell);
						main.enableLogin();
						main.enableReg();
						regShell.dispose();
					}
				}
		);
		
		regBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						String[] sError = {"- Username ", "- Email ", "- Name ", "- Password "};
						String sErrorMessage = "";
						int[] errorInd = {0,0,0,0};
						int iErr = 0;
						String tempUN = UsernameInput.getText();
						String tempEmail = EmailInput.getText();
						String tempName = NameInput.getText();
						String tempPass = PasswordInput.getText();
						if(tempUN == ""){
							iErr = 1;
							errorInd[0] = 1;
						}
						if(tempEmail == ""){
							iErr = 1;
							errorInd[1] = 1;
						}
						if(tempName == ""){
							iErr = 1;
							errorInd[2] = 1;
						}
						if(tempPass == ""){
							iErr = 1;
							errorInd[3] = 1;
						}
						if(iErr == 1){
							for(int i=0;i<errorInd.length;i++){
								if(errorInd[i] == 1){
									sErrorMessage+=sError[i];
								}						
							}
							sErrorMessage+="Missing!";
							MessageDialog.Error(regShell, "Error", sErrorMessage);
						}else{
							if(!UserFuncs.registerUser(tempUN, tempEmail, tempName, tempPass)){
								MessageDialog.Error(regShell, "Error", "Registeration failed, please try again");
							}else{
								main.enableLogin();
								main.enableReg();
								main.removeShell(regShell);
								regShell.dispose();
							}
						}

					}
				}
		);
		
		regShell.pack();
		regShell.open();

		while (!regShell.isDisposed()) {
			if (!regDisplay.readAndDispatch())
				regDisplay.sleep();
		}
	}
}
